package com.vinfai.demo.controller;

import java.io.IOException;
import java.util.Date;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.WebRequest;

import com.vinfai.demo.service.IUserService;
import com.vinfai.hibernate4.entity.User;

@Controller
@RequestMapping(value="/user")
public class UserController {
	
	private static Logger logger = Logger.getLogger(UserController.class);
	
	@Autowired
	@Qualifier("UserService")
	private IUserService userService;
	
	@RequestMapping(value="/revisited",method=RequestMethod.GET,headers="Accept=text/plain")
	public @ResponseBody String simple(){
		logger.error("122222222222222222");
		return "HELLO WORLD revisited!";
	}
	@RequestMapping(value="/validateUser",method=RequestMethod.POST,headers="Accept=text/plain")
	public @ResponseBody String validateUser(String username,String password,HttpServletResponse response){
		logger.error("username:"+username+";pwd:"+password);
		String rs = userService.validateLogin(username, password);
		//M1:使用原始response
		response.setContentType("text/html;charset=UTF-8");
		try {
			response.getWriter().print(rs);
			response.getWriter().flush();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				response.getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
		//M2:修改@ResponseBody调用过程中的StringHttpMessageConverter默认编码 好像不好用
		//return rs;
	} 
	@RequestMapping(value="/login",method=RequestMethod.POST)
	public String login(String username,String password){
		logger.error("login invoking...");
		//return "forward:listuser";
		return "redirect:listuser";
	}
	
	@RequestMapping(value="/listuser",method=RequestMethod.GET)
	public String listUser(){
		logger.error("do........");
		return "user/list_user";
	}
}
